﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;


namespace YTCMS.Core.PDF
{
    public class PDFHeaderAndFooterEvent : PdfPageEventHelper, IPdfPageEvent
    {
        public string watermarkPath = "";
        public int fontsize = 10;
        public PdfTemplate tpl = null;//模版  
        public Font font = null;
        public BaseFont bf = null;
        public PDFHeaderAndFooterEvent(BaseFont _bf, int _fontsize, string _watermarkPath)
        {
            bf = _bf;
            fontsize = _fontsize;
            watermarkPath = _watermarkPath;
            font = new iTextSharp.text.Font(bf, fontsize, iTextSharp.text.Font.NORMAL);
        }
        //重写 关闭一个页面时 
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            if (tpl == null)
                tpl = writer.DirectContent.CreateTemplate(50, 100);
            Phrase footer = new Phrase("第" + (writer.PageNumber) + "页/", font);
            PdfContentByte cb = writer.DirectContent; //模版 显示总共页数 
            cb.AddTemplate(tpl, document.Right - 64 + document.LeftMargin, document.Bottom - 10);//调节模版显示的位置 
            //页脚显示的位置 
            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer, document.Right - 80 + document.LeftMargin, document.Bottom - 10, 0);
            //写水印
            if (!string.IsNullOrEmpty(watermarkPath) && System.IO.File.Exists(watermarkPath))
            {
                //PdfStamper pdfStamper = new PdfStamper(writer, new FileStream(outPath, FileMode.Create));
                //PdfContentByte waterMarkContent;
                iTextSharp.text.Image image = null;
                //Stream s =AppDomain.CurrentDomain.GetAssemblies()[0].GetManifestResourceStream("WatermarkTool.wm.png");
                //image = iTextSharp.text.Image.GetInstance(s);                    

                image = iTextSharp.text.Image.GetInstance(watermarkPath);
                //image = iTextSharp.text.Image.GetInstance(@"E:\longkin_securityfile\templete\project\zhang.png");
                image.SetAbsolutePosition(0, 0);
                writer.DirectContent.AddImage(image);
                //for (int i = 1; i <= numberOfPages; i++)
                //{
                //    waterMarkContent = pdfStamper.GetUnderContent(i);  //GetOverContent
                //    waterMarkContent.AddImage(image);
                //} 
            }
        }
        //重写 打开一个新页面时  
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            if (tpl == null)
                tpl = writer.DirectContent.CreateTemplate(50, 100);
            writer.PageCount = writer.PageNumber;


        }

        //关闭PDF文档时发生该事件 
        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            //iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(@"E:\longkin_securityfile\templete\project\zhang.png");
            //image2.SetAbsolutePosition(0, 0);
            //writer.DirectContent.AddImage(image2);

            if (tpl != null)
            {
                //调用的字体 
                tpl.BeginText();
                tpl.SetFontAndSize(bf, fontsize);
                //tpl.SetTextMatrix(document.Right - 60 + document.LeftMargin, document.Bottom - 10); 
                //生成的模版的字体、颜色 
                tpl.ShowText("共" + (writer.PageNumber - 1) + "页");//(writer.PageNumber - 2).ToString() 
                //模版显示的内容 
                tpl.EndText();
                tpl.ClosePath();
            }
        }
    }
}
